package cn.rongdao.jrkabao.ui.IdentificationVerificate.uicomponent;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mifengkong.frtools.http.request.callback.BaseImpl;
import com.mifengkong.frtools.http.request.callback.FRBaseObserver;
import com.mifengkong.frtools.util.FRSharedPreferences;
import com.mifengkong.frtools.util.FRString;

import cn.rongdao.jrkabao.R;
import cn.rongdao.jrkabao.bean.takephoto.ValidateIDCardInfoByYiTuBean;
import cn.rongdao.jrkabao.constant.SPConstant;
import cn.rongdao.jrkabao.model.takephoto.GetUserIdInfoFromYiTuModel;
import cn.rongdao.jrkabao.util.growingio.BuriedUtil;

/**
 * Created by jack on 2017/10/11
 */

public class IdentifyVerifyUIComponent extends LinearLayout {

    private IdentifyVerifyInputUIComponent nameInputUIComponent;
    private IdentifyVerifyInputUIComponent numberInputUIComponent;
    private TextView tvNext;
    private View viewSpace;

    private boolean isNameEditChange;
    private boolean isNumberEditChange;

    private OnIdentifyVerifyLoanClickListener onIdentifyVerifyLoanClickListener;

    public IdentifyVerifyUIComponent(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        nameInputUIComponent = findViewById(R.id.liv_name);
        numberInputUIComponent = findViewById(R.id.liv_number);
        tvNext = findViewById(R.id.liv_tv_next);
        viewSpace = findViewById(R.id.liv_view);

        nameInputUIComponent.setNameStyle();
        numberInputUIComponent.setNumberStyle();

        initListener();
    }

    public void getNameAndCardNumberInfo(BaseImpl baseImpl) {
        String name = FRSharedPreferences.getStringDefault(SPConstant.NAME, "");
        String number = FRSharedPreferences.getStringDefault(SPConstant.ID_CARD, "");
        if (FRString.isEmpty(name, number)) {
            GetUserIdInfoFromYiTuModel.getInstance().execute(new FRBaseObserver<ValidateIDCardInfoByYiTuBean>(baseImpl, "加载中，请稍候...") {
                @Override
                protected void onBaseNext(ValidateIDCardInfoByYiTuBean data) {
                    setNameAndNumber(FRString.valueOf(data.getName()), FRString.valueOf(data.getCitizen_id()));
                    if (null != onIdentifyVerifyLoanClickListener) {
                        onIdentifyVerifyLoanClickListener.getYiTuStatus(data.getYitu_state());
                    }
                }

                @Override
                protected void onBaseError(Throwable t) {
                }
            });
        } else {
            setNameAndNumber(name, number);
        }
    }

    public void setNameAndNumber(String name, String number) {
        nameInputUIComponent.setEditTextContent(name);
        numberInputUIComponent.setEditTextContent(number);
    }

    private void initListener() {
        nameInputUIComponent.setOnNumberTextChangerListener(new IdentifyVerifyInputUIComponent.OnNumberTextChangerListener() {
            @Override
            public void onChangeListener(boolean isChange) {
                BuriedUtil.kamao_shiming_anniu_apply(getContext(), true);
                isNameEditChange = isChange;
                setNextTextEnable();
            }
        });
        numberInputUIComponent.setOnNumberTextChangerListener(new IdentifyVerifyInputUIComponent.OnNumberTextChangerListener() {
            @Override
            public void onChangeListener(boolean isChange) {
                BuriedUtil.kamao_shiming_anniu_apply(getContext(), false);
                isNumberEditChange = isChange;
                setNextTextEnable();
            }
        });
    }

    private void setNextTextEnable() {
        tvNext.setEnabled(isNameEditChange && isNumberEditChange);
    }

    public void setAddCardStyle() {
        tvNext.setVisibility(GONE);
        viewSpace.setVisibility(View.GONE);
        nameInputUIComponent.setNameEditVisible(false);
        numberInputUIComponent.setNameEditVisible(false);
    }

    public void setNextClickListener(OnClickListener onClickListener) {
        tvNext.setOnClickListener(onClickListener);
    }

    public interface OnIdentifyVerifyLoanClickListener {
        void getYiTuStatus(String status);
    }

    public void setOnIdentifyVerifyLoanClickListener(OnIdentifyVerifyLoanClickListener onIdentifyVerifyLoanClickListener) {
        this.onIdentifyVerifyLoanClickListener = onIdentifyVerifyLoanClickListener;
    }

    public String getName() {
        return nameInputUIComponent.getEditTextContent();
    }

    public String getNumber() {
        return numberInputUIComponent.getEditTextContent();
    }
}